package com.zjy.weather.activity

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.widget.ImageView
import com.zjy.weather.R
import com.zjy.weather.adapter.FragmentAdapter
import com.zjy.weather.base.BaseActivity
import com.zjy.weather.fragment.NoticeFragement
import com.zjy.weather.fragment.WeatherFragement

class MainActivity : BaseActivity() {

    private lateinit var mViewPager: ViewPager
    private lateinit var mWeatherView: ImageView
    private lateinit var mNoticeView: ImageView
    private var mAdapter: FragmentAdapter<Fragment>? = null
    private var mList: MutableList<Fragment>? = null
    private lateinit var mNoticeFragment: NoticeFragement
    private lateinit var mWeahterFragment: WeatherFragement

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        enableTranslucentStatusBar()
        setClipPaddingView(findViewById(R.id.top_ll))
        initView()
        bindEvent()
        initData()
    }

    /**
     * 初始化View
     */
    fun initView() {
        mViewPager = findViewById(R.id.container_vp) as ViewPager
        mWeatherView = findViewById(R.id.weather_iv) as ImageView
        mNoticeView = findViewById(R.id.notice_iv) as ImageView
    }

    /**
     * 事件绑定
     */
    fun bindEvent() {
        mWeatherView.setOnClickListener {
            mViewPager.setCurrentItem(0, false)
        }
        mNoticeView.setOnClickListener {
            mViewPager.setCurrentItem(1, false)
        }

        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageSelected(position: Int) {
                when (position) {
                    0 -> {
                        mWeatherView.setImageResource(R.drawable.weather_press)
                        mNoticeView.setImageResource(R.drawable.weather)
                    }
                    1 -> {
                        mWeatherView.setImageResource(R.drawable.weather)
                        mNoticeView.setImageResource(R.drawable.weather_press)
                    }
                }

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }
        })
    }

    /**
     * 初始化数据
     */
    fun initData() {
        mWeahterFragment = WeatherFragement()
        mNoticeFragment = NoticeFragement()
        mList = mutableListOf(mWeahterFragment, mNoticeFragment);
        mAdapter = FragmentAdapter(supportFragmentManager, mList)
        mViewPager.adapter = mAdapter
    }


}
